<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>天气案例</title>
    <script src="../commonjs/vue.js"></script>
</head>
<body>
    <div id="root">
        <span>今天天气真{{info}}<br/><br/></span>
        <button @click="changeWeather">切换天气</button>
        <hr size="3" color="#f40" />
        a的值：{{numbers.a}}<br/>
        <button @click="numbers.a++">点我a+1</button>
        <br><br>
        b的值：{{numbers.b}}
        <button @click="numbers.b++">点我b+1</button>
        <button @click="numbers={a:100,b:200}">彻底改变numbers</button>
    </div>
    <script>
        Vue.config.productionTip = false;//关闭开发提示
        const vm = new Vue({
            el:'#root',
            data:{
                isHot:true,
                numbers:{
                    a:1,
                    b:1
                }
            },
            computed:{
                info(){
                    return this.isHot ? '炎热' : '寒冷'
                }
            },
            methods:{
                changeWeather(){
                    this.isHot = !this.isHot
                }
            },
            //属性监视第一种写法
            watch:{
                isHot:{
                    immediate:true, //立即执行，不用等值发生改变
                    //isHot发生改变时调用
                    handler(newValue,oldValue){
                        console.log("isHot被修改了！",newValue,oldValue)
                    }
                },
                //监视多级菜单中的某个属性
/*                'numbers.a':{
                    handler(newValue, oldValue) {
                        console.log("numbers.a被改变了！")
                    }
                },*/
                //监视多级结构中所有属性的变化
                numbers:{
                    deep:true,
                    handler(newValue, oldValue) {
                        console.log("numbers完全被改变!")
                    }
                }
            }
        })
        //属性监视第二种写法
/*        vm.$watch('isHot',{
            immediate:true,
            handler(newValue,oldValue){
                console.log("isHot被修改了！",newValue,oldValue)
            }
        })*/
    </script>
</body>
</html>